package academic.ui.client.widgets.forms;

import java.util.Date;

import ui.client.GenericContainerTag;
import ui.client.widgets.InputField;

import academic.ui.client.rules.Rules;
import ecommerce.soco.shared.GWT_Customer;
import ecommerce.soco.shared.GWT_Order;

import com.google.gwt.user.client.ui.PasswordTextBox;

public class RegistrationForm extends FormWidget {
	

	protected InputField name = new InputField("Full Name");
	protected InputField email = new InputField("Email");
	protected PasswordTextBox password1 = new PasswordTextBox();
	
	protected PasswordTextBox password2 = new PasswordTextBox();
	protected GenericContainerTag passwordsDiv = new GenericContainerTag("div");
	
	public RegistrationForm() {
		super();
		div.add(name);
		name.getField().setStyleName("longInput");
		div.add(email);
		email.getField().setStyleName("longInput");
		GenericContainerTag password1Lbl = new GenericContainerTag("span",
		"Password");
		GenericContainerTag password2Lbl = new GenericContainerTag("span",
		"Password");
		GenericContainerTag confirmLbl = new GenericContainerTag("span","(confirm)");
		confirmLbl.setStyleName("grayText");
		password2Lbl.add(confirmLbl);
		
		div.add(passwordsDiv);
		passwordsDiv.setStyleName("field");
		passwordsDiv.add(password1Lbl);
		passwordsDiv.add(password1);
		passwordsDiv.add(password2Lbl);
		passwordsDiv.add(password2);
	}
	
	@Override
	public Boolean checkRules() {
		super.checkRules();
		if("".equals(email.getText()))
		{
			email.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Email is required."));
		}
		if("".equals(password1.getText()))
		{
			password1.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Password is required."));
		}
		if(!"".equals(email.getText()) && !Rules.validEmail(email.getText()))
		{
			email.addStyleName("invalid");
			errorMsgs.add(new GenericContainerTag("div","Email is not valid."));
		}
		if("".equals(name.getText()))
		{
			name.addStyleName("missingValue");
			errorMsgs.add(new GenericContainerTag("div","Fullname is required."));
		}
		if(!password1.getText().equals(password2.getText()))
		{
			passwordsDiv.addStyleName("invalid");
			errorMsgs.add(new GenericContainerTag("div","Password fields do not match"));
		}
		return errorMsgs.getWidgetCount()<1; 
	}
	
	public GWT_Customer getCustomer() {
		GWT_Customer customerReg = new GWT_Customer();
		String[] names = name.getText().split("\\W");
		if(names!=null && names.length>0)
		{
			customerReg.setFirstName(names[0]);
			customerReg.setLastName(names[names.length-1]);
		}
		customerReg.setEmailAddr(email.getText());
		customerReg.setPassword(password1.getText());
		
		//TODO:Might need to add these values to the UI
		customerReg.setGender("N");
		customerReg.setBirthDate(new Date());
		customerReg.setTelephoneNumber("N");
		
		return customerReg;
	}

	@Override
	public void removeErrors() {
		errorMsgs.clear();
		email.removeStyleName("missingValue");
		password1.removeStyleName("missingValue");
		email.removeStyleName("invalid");
		passwordsDiv.removeStyleName("invalid");
	}

	public GWT_Order updateOrder(GWT_Order order) {
		order.customerEmail = email.getText();
		order.custom1 = password1.getText();
		order.billingEmail = email.getText();
		return order;
	}

}
